package com.higold.cloud.ui.adapter

import android.annotation.SuppressLint
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.higold.cloud.R
import android.widget.ImageView
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.higold.cloud.MyApplication.Companion.context
import com.higold.cloud.glide.GlideApp
import com.higold.cloud.mvp.model.bean.dailyPostList.Data
import com.higold.cloud.utils.CircleTransform
import com.higold.cloud.utils.DisplayManager
import java.text.ParsePosition
import java.text.SimpleDateFormat
import java.util.*


/**
 *    author : lxz
 *    date   : 2019/8/12 15:05
 *    desc   :
 */
class ReportAdapter(data: ArrayList<Data>) : BaseQuickAdapter<Data, BaseViewHolder>(R.layout.item_regional, data) {

    private var mContext = context
    private var listener: ImageClickListener? = null

    override fun convert(helper: BaseViewHolder, item: Data) {
//        (helper.getView<View>(R.id.tweetText) as TextView).text = SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create()
//        (helper.getView<View>(R.id.tweetText) as TextView).movementMethod = LinkMovementMethod.getInstance()
        helper.setText(R.id.tv_manager, item.saleman_name)
                .setText(R.id.tv_job, item.saleman_short_name + " - 区域经理")
                .setText(R.id.tv_hot_goods, item.hotsale_product_name)
                .setText(R.id.tv_short_goods, item.stockout_product_name)
                .setText(R.id.tv_shop, item.shop_name)
                .setText(R.id.tv_time, timet(item.createtime.toString()))
                .setText(R.id.tv_address, item.address)
        addView(item, (helper.getView<View>(R.id.ll_pic) as LinearLayout))
    }

    private fun addView(item: Data, view: LinearLayout) {
        view.removeAllViews()
        val imageUrl = stringToList(item.images)


        if (imageUrl.isNotEmpty())
            for (i in 0 until imageUrl.size) {
                val child = ImageView(context)

                GlideApp.with(context)
                        .load(imageUrl[i])
                        .placeholder(R.drawable.ic_load_p)
                        .skipMemoryCache(true)
                        .centerCrop()
                        .transition(DrawableTransitionOptions().crossFade())
                        .transform(CircleTransform(context))
                        .thumbnail(0.5f)
                        .into(child)

                child.setPadding(DisplayManager.dip2px(6f)!!, 0, DisplayManager.dip2px(10f)!!, 0)
                child.setOnClickListener {
                    Log.e("看看position", i.toString())
                    listener!!.ImageClick(i, child, imageUrl)
                }
                view.addView(child)
            }

    }

    internal var clickableSpan: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
        }

        override fun updateDrawState(ds: TextPaint) {
//            ds.color = Utils.getContext().getResources().getColor(R.color.clickspan_color)
//            ds.isUnderlineText = true
        }

    }

    @SuppressLint("SimpleDateFormat")
    fun timet(time: String): String {
        val sdr = SimpleDateFormat("yyyy年MM月dd日  HH:mm")
        val lcc = java.lang.Long.valueOf(time)
        val i = Integer.parseInt(time)
        return sdr.format(Date(i * 1000L))
    }

    fun stringToList(strs: String): Array<String> {
        val str = strs.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        return str
    }

    interface ImageClickListener {
        fun ImageClick(position: Int, imageView: ImageView, imageUrl: Array<String>)
    }

    fun setOnImageClickListener(listener: ImageClickListener) {
        this.listener = listener
    }


}
